package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoArtColleges;
import com.muyu.danmo.domain.DanmoArtMajors;
import com.muyu.danmo.mapper.DanmoArtCollegesMapper;
import com.muyu.danmo.mapper.DanmoArtMajorsMapper;
import com.muyu.danmo.service.IDanmoArtMajorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学院专业Service业务层处理
 * 
 * @author yangle
 * @date 2024-11-26
 */
@Service
public class DanmoArtMajorsServiceImpl implements IDanmoArtMajorsService
{
    @Autowired
    private DanmoArtMajorsMapper danmoArtMajorsMapper;


    /**
     * 查询学院专业
     * 
     * @param majorId 学院专业主键
     * @return 学院专业
     */
    @Override
    public DanmoArtMajors selectDanmoArtMajorsByMajorId(Long majorId)
    {
        return danmoArtMajorsMapper.selectDanmoArtMajorsByMajorId(majorId);
    }

    /**
     * 查询学院专业列表
     * 
     * @param danmoArtMajors 学院专业
     * @return 学院专业
     */
    @Override
    public List<DanmoArtMajors> selectDanmoArtMajorsList(DanmoArtMajors danmoArtMajors)
    {
        return danmoArtMajorsMapper.selectDanmoArtMajorsList(danmoArtMajors);
    }

    /**
     * 新增学院专业
     * 
     * @param danmoArtMajors 学院专业
     * @return 结果
     */
    @Override
    public int insertDanmoArtMajors(DanmoArtMajors danmoArtMajors)
    {
        danmoArtMajors.setCreateTime(DateUtils.getNowDate());
        return danmoArtMajorsMapper.insertDanmoArtMajors(danmoArtMajors);
    }

    /**
     * 修改学院专业
     * 
     * @param danmoArtMajors 学院专业
     * @return 结果
     */
    @Override
    public int updateDanmoArtMajors(DanmoArtMajors danmoArtMajors)
    {
        danmoArtMajors.setUpdateTime(DateUtils.getNowDate());
        return danmoArtMajorsMapper.updateDanmoArtMajors(danmoArtMajors);
    }

    /**
     * 批量删除学院专业
     * 
     * @param majorIds 需要删除的学院专业主键
     * @return 结果
     */
    @Override
    public int deleteDanmoArtMajorsByMajorIds(Long[] majorIds)
    {
        return danmoArtMajorsMapper.deleteDanmoArtMajorsByMajorIds(majorIds);
    }

    /**
     * 删除学院专业信息
     * 
     * @param majorId 学院专业主键
     * @return 结果
     */
    @Override
    public int deleteDanmoArtMajorsByMajorId(Long majorId)
    {
        return danmoArtMajorsMapper.deleteDanmoArtMajorsByMajorId(majorId);
    }

    @Autowired
    private DanmoArtCollegesMapper danmoArtCollegesMapper;
    @Override
    public Result collectionColleges(String name) {
       List<DanmoArtMajors> danmoArtMajors = danmoArtMajorsMapper.collectionColleges(name);
       if (danmoArtMajors.size() == 0){
           return Result.error("没有该学院专业");
       }
        List<DanmoArtColleges> danmoArtCollegesList = new ArrayList<>();
        for (DanmoArtMajors danmoArtMajor : danmoArtMajors) {
            Long collegesId = danmoArtMajor.getCollegesId();
          DanmoArtColleges danmoArtColleges =    danmoArtCollegesMapper.selectDanmoArtCollegesByIdOne(collegesId);
            danmoArtCollegesList.add(danmoArtColleges);
        }
        return Result.success(danmoArtCollegesList);
    }

    @Override
    public List<Map<String, Object>> selectDanmoArtMajorsListtt(DanmoArtMajors danmoArtMajors) {
        List<DanmoArtMajors> danmoArtMajors1 = danmoArtMajorsMapper.selectDanmoArtMajorsList(danmoArtMajors);

        Map<String, Map<String, Object>> departmentMap = new HashMap<>();

        for (DanmoArtMajors artMajors : danmoArtMajors1) {
            String departmentName = artMajors.getDepartmentName();
            String departmentLevel = artMajors.getDepartmentLevel();

            // 将 DanmoArtMajors 对象直接添加到 majors 列表中
            // 这里不需要进行转换，直接操作 DanmoArtMajors 对象

            // 确保 departmentName 已经存在于 departmentMap 中
            Map<String, Object> departmentInfo = departmentMap.computeIfAbsent(departmentName, k -> new HashMap<>());

            // 如果 departmentInfo 中没有 "majors" 字段，先初始化它
            if (!departmentInfo.containsKey("majors")) {
                departmentInfo.put("departmentName", departmentName);
                departmentInfo.put("departmentLevel", departmentLevel);
                departmentInfo.put("majors", new ArrayList<DanmoArtMajors>()); // majors 存储 DanmoArtMajors 对象
            }

            // 获取 majors 列表并添加新的专业信息
            List<DanmoArtMajors> majorsList = (List<DanmoArtMajors>) departmentInfo.get("majors");
            majorsList.add(artMajors);  // 添加新的专业 (DanmoArtMajors)
        }

// 将 departmentMap 中的值转换为 List
        List<Map<String, Object>> departmentInfoList = new ArrayList<>(departmentMap.values());

        return departmentInfoList;
       }
    }
